package com.bkbw.live.database;

import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JavaIdentifierTransformer;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bkbw.live.database.util.DatabasePool;
import com.bkbw.live.database.util.DatabaseUtil;
import com.bkbw.live.battle.database.bean.BallBean;
import com.bkbw.live.battle.database.bean.DBagItemBean;
import com.bkbw.live.battle.database.bean.LevelExpBean;
import com.bkbw.live.battle.database.bean.RoleBean;
import com.bkbw.live.battle.database.bean.TableTemplate;
import com.bkbw.live.battle.database.bean.VipBean;
import com.bkbw.live.battle.database.bean.WeaponBean;
import com.bkbw.live.battle.database.bean.WeaponHotBean;
import com.bkbw.live.battle.database.bean.WeaponsBean;
import com.bkbw.live.battle.database.bean.DBagItemBean;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagEquipment;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagItem;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBagWeapon;
import com.bkbw.live.common.dataproto.BattleToDatabasePb.DbBodyEquipment;
import com.bkbw.live.common.dataproto.CommonMessagePb.DCharacterPb;
import com.bkbw.live.common.util.Utils;

public class DatabaseMain {
	static class Weapon {
		int Id;
		
		String nameString;
		int weaponType;
		public int getId() {
			return Id;
		}
		public void setId(int Id) {
			this.Id = Id;
		}
		public String getNameString() {
			return nameString;
		}
		public void setNameString(String nameString) {
			this.nameString = nameString;
		}
		public int getWeaponType() {
			return weaponType;
		}
		public void setWeaponType(int weaponType) {
			this.weaponType = weaponType;
		}
		
		@Override
		public String toString() {
			return "id "+Id+" name "+nameString+" weapon "+weaponType;
		}
	}
	
	
	static Logger logger = LoggerFactory.getLogger("mylog");
	public static void main(String args[]) {
		PropertyConfigurator.configure("config/log4j.properties");
		PropertyConfigurator.configure("config/jdbc.properties");
		logger.debug("hehea");
		try {
			/*
			String jsonString = "{\"id\":1,\"name\":\"oneal\",\"Score\":93,\"BirthOfYear\":1972,\"BigWRITE\":99}";
			JSONObject jsonObject = JSONObject.fromObject(jsonString);
			logger.debug(jsonObject.toString());
			
			JsonConfig config = new JsonConfig();
	        config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {

	            @Override
	            public String transformToJavaIdentifier(String str) {
	                char[] chars = str.toCharArray();
	                chars[0] = Character.toLowerCase(chars[0]);
	                return new String(chars);
	            }

	        });
	        config.setRootClass(TestBean.class);
			
			TestBean bean = (TestBean)JSONObject.toBean(jsonObject, config);
			logger.debug("bean " + bean);
			
			TestBean tb2 = new TestBean();
			tb2.setId(2);
			tb2.setName("kobe");
			tb2.setBirthOfYear(1978);
			tb2.setScore(92);
			
			logger.debug("tb2 " + JSONObject.fromObject(tb2).toString());
			*/
			
			Connection conn = DatabasePool.getConnection();
			//JSONArray jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_weapon_hot");
			//TableTemplate<Integer, WeaponHotBean> tb = new TableTemplate<Integer, WeaponHotBean>(jsonArray, "Id", WeaponHotBean.class){};
			//logger.debug(tb.toString());
			
			
			JSONArray jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_weapons");
			TableTemplate<Integer, WeaponsBean> tb2 = new TableTemplate<Integer, WeaponsBean>(jsonArray, "Id", WeaponsBean.class){};
			logger.debug(tb2.toString());
			
			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from d_bagitems");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				DBagItemBean bean = (DBagItemBean)JSONObject.toBean(jsonObject, DBagItemBean.class);
				logger.debug(bean.toString());
			}
			logger.debug("--------");
			//ResultSet rs0 = DatabaseUtil.executeQueryInTransReturnRs(conn, "select * from d_bagitems");
			//jsonArray = DatabaseUtil.resultSetToJson(rs0);
			//logger.debug(jsonArray.toString());
			
			List<DBagItemBean> list = DatabaseUtil.executeQueryInTransReturnBean(conn, "select * from d_bagitems", DBagItemBean.class);
			//List<DBagItemBean> list = DatabaseUtil.resultSetToBean(rs0, DBagItemBean.class);
			for (DBagItemBean dBagItemBean : list) {
				logger.debug("bagitem : " + dBagItemBean);
			}
			
			//logger.debug(tb2.toString());
			
//			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_role");
//			TableTemplate<Integer, RoleBean> tb3 = new TableTemplate<Integer, RoleBean>(jsonArray, "Id", RoleBean.class){};
//			//logger.debug(jsonArray.toString());
//			logger.debug(tb3.toString());
//			
//			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_ball");
//			TableTemplate<Integer, BallBean> tb4 = new TableTemplate<Integer, BallBean>(jsonArray, "Id", BallBean.class){};
//			//logger.debug(jsonArray.toString());
//			logger.debug(tb4.toString());
//			
//			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_vip");
//			TableTemplate<Integer, VipBean> tb5 = new TableTemplate<Integer, VipBean>(jsonArray, "VipLevel", VipBean.class){};
//			logger.debug(tb5.toString());
//			
//			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_levelexp");
//			TableTemplate<Integer, LevelExpBean> tb6 = new TableTemplate<Integer, LevelExpBean>(jsonArray, "Level", LevelExpBean.class){};
//			//logger.debug(jsonArray.toString());
//			logger.debug(tb6.toString());
			
			// call procedure
			CallableStatement callableStatement = conn.prepareCall("{call proc_read_character(?,?)}");
    		callableStatement.setInt(1, 1);
    		callableStatement.registerOutParameter(2, java.sql.Types.INTEGER); 
    		ResultSet rs=null;
    		boolean hasResultSet = callableStatement.execute(); 
    		int userId = callableStatement.getInt(2);
    		System.out.println("userid " + userId);
    		if (hasResultSet) {
    			/*rs = callableStatement.getResultSet();
				JSONArray procJsonArrays = DatabaseUtil.resultSetToJson(rs);
				logger.debug(procJsonArrays.toString());
    			while (callableStatement.getMoreResults()) {
    				rs = callableStatement.getResultSet();
    				procJsonArrays = DatabaseUtil.resultSetToJson(rs);
    				logger.debug(procJsonArrays.toString());
    			}*/
    			rs = callableStatement.getResultSet();
    			DbBagEquipment.Builder builder = DbBagEquipment.newBuilder();
    			
    			//builder.setId(value)
    			
    			List<DCharacterPb> list1 = DatabaseUtil.resultSetToProtoBean(rs, DCharacterPb.class, DCharacterPb.Builder.class);
    			Utils.dumpList(list1);
    			
    			callableStatement.getMoreResults();
    			rs = callableStatement.getResultSet();
    			List<DbBodyEquipment> list2 = DatabaseUtil.resultSetToProtoBean(rs, DbBodyEquipment.class, DbBodyEquipment.Builder.class);
    			Utils.dumpList(list2);
    			
    			callableStatement.getMoreResults();
    			rs = callableStatement.getResultSet();
    			List<DbBagItem> list3 = DatabaseUtil.resultSetToProtoBean(rs, DbBagItem.class, DbBagItem.Builder.class);
    			Utils.dumpList(list3);
    			
    			callableStatement.getMoreResults();
    			rs = callableStatement.getResultSet();
    			List<DbBagEquipment> list4 = DatabaseUtil.resultSetToProtoBean(rs, DbBagEquipment.class, DbBagEquipment.Builder.class);
    			Utils.dumpList(list4);
    			
    			callableStatement.getMoreResults();
    			rs = callableStatement.getResultSet();
    			List<DbBagWeapon> list5 = DatabaseUtil.resultSetToProtoBean(rs, DbBagWeapon.class, DbBagWeapon.Builder.class);
    			Utils.dumpList(list5);
    			
    		}
    		
			
			DatabasePool.release(conn);
			
			/*JSONObject jsonObject = jsonArray.getJSONObject(0);
			Object object = JSONObject.toBean(jsonObject, TestBean2.class);
			TestBean2 testbean = null;
			if (object instanceof TestBean2) {
				testbean = (TestBean2)object;
				logger.debug("testbean succ " + testbean.toString());
				
			}
			else {
				logger.debug("testbean fail");
			}*/
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public static void testTables() {
		try {
			Connection conn = DatabasePool.getConnection();
			JSONArray jsonArray = null;
			
			
			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_weapon_hot");
			TableTemplate<Integer, WeaponHotBean> tb = new TableTemplate<Integer, WeaponHotBean>(jsonArray, "Id", WeaponHotBean.class){};
			logger.debug(tb.toString());
			
			
			jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_weapons");
			TableTemplate<Integer, WeaponsBean> tb2 = new TableTemplate<Integer, WeaponsBean>(jsonArray, "Id", WeaponsBean.class){};
			logger.debug(tb2.toString());
			
	//		jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_role");
	//		TableTemplate<Integer, RoleBean> tb3 = new TableTemplate<Integer, RoleBean>(jsonArray, "Id", RoleBean.class){};
	//		//logger.debug(jsonArray.toString());
	//		logger.debug(tb3.toString());
	//		
	//		jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_ball");
	//		TableTemplate<Integer, BallBean> tb4 = new TableTemplate<Integer, BallBean>(jsonArray, "Id", BallBean.class){};
	//		//logger.debug(jsonArray.toString());
	//		logger.debug(tb4.toString());
	//		
	//		jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_vip");
	//		TableTemplate<Integer, VipBean> tb5 = new TableTemplate<Integer, VipBean>(jsonArray, "VipLevel", VipBean.class){};
	//		logger.debug(tb5.toString());
	//		
	//		jsonArray = DatabaseUtil.executeQueryInTrans(conn, "select * from mq_levelexp");
	//		TableTemplate<Integer, LevelExpBean> tb6 = new TableTemplate<Integer, LevelExpBean>(jsonArray, "Level", LevelExpBean.class){};
	//		//logger.debug(jsonArray.toString());
	//		logger.debug(tb6.toString());
			
			DatabasePool.release(conn);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
}
